package com.ruoyi.wechat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.wechat.common.Constants;
import com.ruoyi.wechat.common.ServiceException;
import com.ruoyi.wechat.domain.AppUser;
import com.ruoyi.wechat.domain.UserAddr;
import com.ruoyi.wechat.mapper.UserAddrMapper;
import com.ruoyi.wechat.service.IUserAddrService;
import com.ruoyi.wechat.utils.LoginHelper;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserAddrServiceImpl extends ServiceImpl<UserAddrMapper, UserAddr> implements IUserAddrService {

    @Resource
    private UserAddrMapper userAddrMapper;

    @Override
    public UserAddr detail(Long id) {
        AppUser appUser = LoginHelper.getSession().getAppUser();
        LambdaQueryWrapper<UserAddr> addrQuery = new LambdaQueryWrapper<>();
        addrQuery.eq(UserAddr::getUserId, appUser.getId());
        addrQuery.eq(UserAddr::getId, id);
        UserAddr userAddr = userAddrMapper.selectOne(addrQuery);
        return userAddr;
    }

    @Override
    public void edit(UserAddr form) {
        AppUser appUser = LoginHelper.getSession().getAppUser();
        UserAddr userAddr = userAddrMapper.selectById(form.getId());
        if (!appUser.getId().equals(userAddr.getUserId())) {
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "没有权限");
        }
        userAddr.setId(form.getId());
        userAddr.setArea(form.getArea());
        userAddr.setComy(form.getComy());
        userAddr.setRidgepole(form.getRidgepole());
        userAddr.setDoor(form.getDoor());
        userAddr.setAddrId(form.getAddrId());
        userAddr.setIsFirst(form.getIsFirst());
        userAddr.setDetail(form.getDetail());
        userAddr.setNick(form.getNick());
        userAddr.setPhone(form.getPhone());


        if (Constants.Common.STATUS_1.getValue().equals(form.getIsFirst())) {
            LambdaQueryWrapper<UserAddr> listQuery = new LambdaQueryWrapper<>();
            listQuery.eq(UserAddr::getUserId, appUser.getId());
            List<UserAddr> userAddrList = userAddrMapper.selectList(listQuery);
            for (UserAddr addr : userAddrList) {
                addr.setIsFirst(Constants.Common.STATUS_0.getValue());
            }
            userAddrList.add(userAddr);
            this.updateBatchById(userAddrList);
            return;
        }
        userAddrMapper.updateById(userAddr);
    }

    @Override
    public void add(UserAddr form) {
        AppUser appUser = LoginHelper.getSession().getAppUser();
        form.setUserId(appUser.getId());
        form.setTitle(form.getDetail());
        //设置默认地址
        if (Constants.Common.STATUS_1.getValue().equals(form.getIsFirst())) {
            LambdaQueryWrapper<UserAddr> listQuery = new LambdaQueryWrapper<>();
            listQuery.eq(UserAddr::getUserId, appUser.getId());
            List<UserAddr> userAddrList = userAddrMapper.selectList(listQuery);
            for (UserAddr addr : userAddrList) {
                addr.setIsFirst(Constants.Common.STATUS_0.getValue());
            }
            this.updateBatchById(userAddrList);
        }
        userAddrMapper.insert(form);
    }

    @Override
    public void delete(Long id) {
        AppUser appUser = LoginHelper.getSession().getAppUser();
        UserAddr userAddr = userAddrMapper.selectById(id);
        if (ObjectUtils.isEmpty(userAddr)) {
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "地址已被删除");
        }
        if (!userAddr.getUserId().equals(appUser.getId())) {
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "没有权限");
        }
        userAddrMapper.deleteById(userAddr.getId());
    }

    @Override
    public void setFirst(Long userId, Long addrId) {
        LambdaQueryWrapper<UserAddr> query = new LambdaQueryWrapper<>();
        query.eq(UserAddr::getUserId, userId);
        List<UserAddr> myAddrList = userAddrMapper.selectList(query);
        myAddrList.forEach(addr -> {
            addr.setIsFirst(0);
            if (addrId.equals(addr.getId())) {
                addr.setIsFirst(1);
            }
        });
        this.updateBatchById(myAddrList);
    }
}
